package cz.cvut.fel.nur.rcp;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.format.DateFormat;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import cz.cvut.fel.nur.rcp.classes.IngredientWrapper;

public class SpizDetail extends Activity implements OnClickListener {

	GlobalClass appState;

	Button menuUp, menuDown, spizDetailButton;

	EditText mnozstviV;
	
	ImageView pantry, recipes, list, options, home, addToCalendar, addToList;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.spizdetail);
		super.onCreate(savedInstanceState);
		
		appState = ((GlobalClass) getApplicationContext());
		
		TextView nazevV = (TextView) findViewById(R.id.spizDetailNazev);
		mnozstviV = (EditText) findViewById(R.id.spizDetailMnozstvi);
		TextView spotrebaV = (TextView) findViewById(R.id.spizDetailSpotreba);
		
		IngredientWrapper iw = appState.getModifiedIngredient();
		nazevV.setText(iw.ingredient.getName());
		mnozstviV.setHint(iw.quantity + iw.ingredient.getQuantityType().meaning);
		
		java.text.DateFormat dfDate_day = DateFormat.getDateFormat(this);
	    String dt = "";	    
	    dt = dfDate_day.format(iw.ingredient.getDefaultExpiration());
	    spotrebaV.setText(dt);
	    
	    
	    spizDetailButton = (Button) findViewById(R.id.spizDetailButton);
	    spizDetailButton.setOnClickListener((android.view.View.OnClickListener) this);
		
		menuDown = (Button) findViewById(R.id.menuDownButton);
		menuDown.setOnClickListener((android.view.View.OnClickListener) this);
		menuUp = (Button) findViewById(R.id.menuUpButton);
		menuUp.setOnClickListener((android.view.View.OnClickListener) this);
		
		pantry = (ImageView) findViewById(R.id.pantryButton);
		pantry.setOnClickListener((android.view.View.OnClickListener) this);
		recipes = (ImageView) findViewById(R.id.recipesButton);
		recipes.setOnClickListener((android.view.View.OnClickListener) this);
		list = (ImageView) findViewById(R.id.listButton);
		list.setOnClickListener((android.view.View.OnClickListener) this);
		options = (ImageView) findViewById(R.id.optionsButton);
		options.setOnClickListener((android.view.View.OnClickListener) this);
		home = (ImageView) findViewById(R.id.homeButton);
		home.setOnClickListener((android.view.View.OnClickListener) this);

		// visibility
		menuDown.setVisibility(View.GONE); // 0 is visible
		pantry.setVisibility(View.GONE);
		recipes.setVisibility(View.GONE);
		list.setVisibility(View.GONE);
		options.setVisibility(View.GONE);
		home.setVisibility(View.GONE);

	}

	public void onClick(View v) {
		if (v.getId() == R.id.menuDownButton) {
			menuDown.setVisibility(View.GONE);
			pantry.setVisibility(View.GONE);
			recipes.setVisibility(View.GONE);
			list.setVisibility(View.GONE);
			options.setVisibility(View.GONE);
			home.setVisibility(View.GONE);
		}	
		if (v.getId() == R.id.menuUpButton) {
			menuDown.setVisibility(0); 
			pantry.setVisibility(0);
			recipes.setVisibility(0);
			list.setVisibility(0);
			options.setVisibility(0);
			home.setVisibility(0);
		}	
		
//		if (v.getId() == R.id.spizDetailMnozstvi) {
//			mnozstviV.setText("");
//		}
//		
		if (v.getId() == R.id.spizDetailButton) {
			IngredientWrapper iw = appState.getModifiedIngredient();
			Editable text = mnozstviV.getText();
			int q = 0;
			if(text.length() > 0) {
				q = Integer.parseInt(text.toString());
			} else {
				q = iw.quantity;
			}
			appState.setIngredientInPantryQuantity(iw.ingredient, q);
			appState.setModifiedIngredient(null);
			this.finish();
		}
		
		/* obrazky */
		if (v.getId() == R.id.pantryButton) {
			startActivity(new Intent(this, Spiz.class));
		}
		if (v.getId() == R.id.recipesButton) {
			startActivity(new Intent(this, Recepty.class));
		}
		if (v.getId() == R.id.listButton) {
			startActivity(new Intent(this, NakupniSeznam.class));
		}
		if (v.getId() == R.id.optionsButton) {
			startActivity(new Intent(this, Nastaveni.class));
		}
		if (v.getId() == R.id.homeButton) {
			startActivity(new Intent(this, Main.class));
		}
	}

}
